package com.guage.producter.controller;

import java.math.BigDecimal;
import java.sql.Timestamp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.guage.producter.domain.SampleTable;
import com.guage.producter.result.JSONResult;
import com.guage.producter.service.ISampleTableService;
import com.guage.producter.utils.SnowflakeIdWorker;
import com.pad.dpscn.daag.sdk.core.annotation.DataSendPosition;

/**
 * @desc
 * @author yangdezhao
 * @since 2024-03-16
 */
@RestController
@RequestMapping("/sampleTable")
@DataSendPosition
public class SampleTableController {

	@Autowired
	public ISampleTableService sampleTableService;

	/**
	 * 保存和修改操作公用此方法
	 * 
	 * @param sampleTable 前端传递来的实体数据
	 */
	@PostMapping(value = "/save")
	public JSONResult save(@RequestBody SampleTable sampleTable) {
		java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
		// 获取当前日期和时间
		java.util.Date now = new java.util.Date();
		// 将Date转换为Timestamp
		Timestamp timestamp = new Timestamp(now.getTime());
		sampleTable.setHireDate(sqlDate);
		sampleTable.setCreateTimeZone(timestamp);
		sampleTable.setCreateTimestamp(timestamp);
		if (sampleTable.getId() != null) {
			sampleTableService.updateById(sampleTable);
		} else {
			SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);
			Long id = idWorker.nextId();
			sampleTable.setId(BigDecimal.valueOf(id));
			sampleTableService.insert(sampleTable);
		}
		return JSONResult.success();
	}

	/**
	 * 根据ID删除指定对象信息
	 * 
	 * @param id
	 */
	@DeleteMapping(value = "/delete/{id}")
	public JSONResult delete(@PathVariable("id") BigDecimal id) {
		sampleTableService.deleteById(id);
		return JSONResult.success();
	}

	// 根据ID查询对象详情信息
	@GetMapping(value = "/selectById/{id}")
	public JSONResult get(@PathVariable("id") String id) {
		return JSONResult.success(sampleTableService.selectById(id));
	}

	/**
	 * 查看所有对象数据（不分页）
	 */
	@GetMapping(value = "/list")
	public JSONResult list() {
		return JSONResult.success(sampleTableService.selectList(null));
	}
	
	@PostMapping(value = "/updateSampleTableByMultipleConditions")
	public JSONResult updateById(@RequestBody SampleTable sampleTable) {
		java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
		// 获取当前日期和时间
		java.util.Date now = new java.util.Date();
		// 将Date转换为Timestamp
		Timestamp timestamp = new Timestamp(now.getTime());
		sampleTable.setHireDate(sqlDate);
		sampleTable.setCreateTimeZone(timestamp);
		sampleTable.setCreateTimestamp(timestamp);
		sampleTableService.updateSampleTableByMultipleConditions(sampleTable);
		return JSONResult.success();
	}
}